package com.leo.foodmaster.commons.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.leo.foodmaster.commons.handler.FieldFillHandler;

import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.Properties;

/**
 * <p>
 * MybatisPlus配置类
 * </p>
 *
 * @author ：Leo
 * @since ：2021-06-25 10:25
 */
@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisPlusConfig {

    /**
     * 分页插件
     * @return PaginationInterceptor
     */
    @Bean
    @ConditionalOnMissingBean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setLimit(-1);
        return paginationInterceptor;
    }

    /**
     * 公共字段自动填充
     * @return MetaObjectHandler
     */
    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new FieldFillHandler();
    }

    @Bean
    @Profile({"dev", "test"})
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
        return new PerformanceMonitorInterceptor();
    }

    /**
     * 注册自定义拦截器
     */
//    @Bean
//    public MybatisInterceptor mybatisInterceptor() {
//        MybatisInterceptor interceptor = new MybatisInterceptor();
//        Properties properties = new Properties();
//        // 可以调用properties.setProperty方法来给拦截器设置一些自定义参数
//        interceptor.setProperties(properties);
//        return interceptor;
//    }


}
